import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { SignService } from './../../../core/service/sign.service';
import { TokenService } from './../../../core/token/token.service';
import { UserInfoService } from './../../../core/service/user-info.service';
import { RegionNumber } from 'src/app/model';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
  providers: [
    SignService
  ]
})
export class LoginComponent implements OnInit {

  index = 0;

  emailForm: FormGroup;
  tellphoneForm: FormGroup;

  regionNumbers: RegionNumber[] = [];

  constructor(
    private fb: FormBuilder,
    private router: Router,
    private i18nService: I18nService,
    private signService: SignService,
    private message: NzMessageService,
    private userInfoService: UserInfoService
  ) { }

  ngOnInit() {
    this.initEmailForm();
    // this.initTellphoneForm();
    // 先初始化电话区号在去初始化手机表单
    this.initRegionNumbers();
  }

  submit() {
    const map =  new Map<number, { pwd: string} & ( {email: string} | { mobile: string})>();
    map.set(0, {
      pwd: this.emailForm.get('password').value,
      email: this.emailForm.get('email').value,
    });
    map.set(1, {
      pwd: this.tellphoneForm.get('password').value,
      mobile: this.tellphoneForm.get('regionNumber').value + '-' + this.tellphoneForm.get('tellphone').value,
    });
    const data = map.get(this.index);
    this.signService.sign(data).subscribe(res => {
      if (res.status === 0) {
        this.userInfoService.saveUserInfo(res.data);
        this.router.navigateByUrl('/');
      } else {
        this.message.create('error', res.message);
      }
    }, );
  }

  toPage(url: string) {
    this.router.navigateByUrl(url);
  }


  private initEmailForm() {
    this.emailForm = this.fb.group({
      // tslint:disable-next-line:max-line-length
      email: [null, [Validators.required, Validators.pattern(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)]],
      password: [null, [Validators.required]],
    });
  }

  private initTellphoneForm() {
    this.tellphoneForm = this.fb.group({
      tellphone: [null, [Validators.required]],
      regionNumber: [(!this.regionNumbers || this.regionNumbers.length === 0) ? null : this.regionNumbers[0].number, [Validators.required]],
      password: [null, [Validators.required]],
    });
  }

  private initRegionNumbers() {
    this.i18nService.getRegionNumbers().subscribe(res => {
      if (res.status === 0) {
        this.regionNumbers = res.data;
      } else {
        this.message.create('error', res.message);
      }
      if (!this.regionNumbers || this.regionNumbers.length === 0) {
        this.message.create('warning', 'region numbers is empty');
      } else {
        // this.seletedRegionNumber = '' + this.regionNumbers[0].number;
        // this.phoneForm.patchValue({ regionNumber: '' + this.regionNumbers[0].number});
      }
    }, error => this.message.create('error', JSON.stringify(error)), () => {
      this.initTellphoneForm();
    });
  }

}
